package com.ky.course.controller;


import com.ky.common.config.SystemConfig;
import com.ky.common.param.AddressAdd;
import com.ky.common.vo.R;
import com.ky.course.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 地址 前端控制器
 * </p>
 *
 * @author zpa
 * @since 2024-06-18
 */
@RestController
@RequestMapping("/course/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 根据用户id查询地址
     * @param request
     * @return
     */
    @RequestMapping("/getAddressByUserId")
    public R getAddressList(HttpServletRequest request){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        if(token==null){
            return R.fail("用户未登录");
        }
        return R.ok(addressService.getAddressList(token));
    }

    /**
     * 添加用户的地址
     * @param request
     * @param addAddress
     * @return
     */
    @RequestMapping("/addAddress")
    public R addAddress(HttpServletRequest request, @RequestBody AddressAdd addAddress){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        return  addressService.addAddress(token,addAddress)?R.ok("添加成功"):R.fail("添加失败");
    }

    /**
     * 修改用户地址
     * @param request
     * @param addressAdd
     * @return
     */
    @RequestMapping("/updateAddress")
    public R updateAddress(HttpServletRequest request,@RequestBody AddressAdd addressAdd){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        return addressService.updateAddress(token,addressAdd)?R.ok("修改成功"):R.fail("修改失败");
    }

    /**
     * 删除用户的地址
     * @param request
     * @param addressAdd
     * @return
     */
    @RequestMapping("/deleteAddress")
    public R deleteAddress(HttpServletRequest request,@RequestBody AddressAdd addressAdd){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        return addressService.deleteAddress(token,addressAdd)?R.ok("删除成功"):R.fail("删除失败");

    }

    /**
     * 根据地址id查询地址
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/getByAddressId")
    public R getByAddressId(HttpServletRequest request, @RequestParam("id") String id){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        return R.ok(addressService.getByAddressId(token,id));
    }

    @RequestMapping("/getDefaultAddress")
    public R getDefaultAddress(HttpServletRequest request){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        R  result = addressService.getDefaultAddress(token);
        return result;
    }
}

